package org.kabeja.parser.objects;

import org.kabeja.dxf.DXFConstants;
import org.kabeja.dxf.objects.DXFMLineStyle;
import org.kabeja.dxf.objects.DXFMLineStyleElement;
import org.kabeja.dxf.objects.DXFObject;
import org.kabeja.parser.DXFValue;

/**
 * @author shutao
 * @since 2022/4/25 12:24
 */
public class DXFMLineStyleHandler extends AbstractDXFObjectHandler {
    public static final int GROUPCODE_MLINE_STYLE_NAME = 2;
    public static final int GROUPCODE_MLINE_STYLE_FLAGS = 70;
    public static final int GROUPCODE_MLINE_STYLE_DESCRIPTION = 3;
    public static final int GROUPCODE_MLINE_STYLE_FILL_COLOR = 62;
    public static final int GROUPCODE_MLINE_STYLE_START_ANGLE = 51;
    public static final int GROUPCODE_MLINE_STYLE_END_ANGLE = 52;
    public static final int GROUPCODE_MLINE_STYLE_ELEMENT_COUNT = 71;
    public static final int GROUPCODE_MLINE_STYLE_ELEMENT_OFFSET = 49;
    public static final int GROUPCODE_MLINE_STYLE_ELEMENT_COLOR = 62;
    public static final int GROUPCODE_MLINE_STYLE_ELEMENT_LINE_STYLE = 6;
    protected DXFMLineStyle style;
    protected DXFMLineStyleElement element;
    protected boolean processLineElement = false;

    public DXFMLineStyleHandler() {
    }

    @Override
    public void endObject() {
    }

    @Override
    public DXFObject getDXFObject() {
        return this.style;
    }

    @Override
    public String getObjectType() {
        return DXFConstants.OBJECT_TYPE_MLINESTYLE;
    }

    @Override
    public void parseGroup(int var1, DXFValue var2) {
        switch(var1) {
            case GROUPCODE_MLINE_STYLE_NAME:
                this.style.setName(var2.getValue());
                break;
            case GROUPCODE_MLINE_STYLE_DESCRIPTION:
                this.style.setDescrition(var2.getValue());
                break;
            case GROUPCODE_MLINE_STYLE_ELEMENT_LINE_STYLE:
                this.element.setLineType(var2.getValue());
                break;
            case GROUPCODE_MLINE_STYLE_ELEMENT_OFFSET:
                this.element = new DXFMLineStyleElement();
                this.element.setOffset(var2.getDoubleValue());
                this.style.addDXFMLineStyleElement(this.element);
                this.processLineElement = true;
                break;
            case GROUPCODE_MLINE_STYLE_START_ANGLE:
                this.style.setStartAngle(var2.getDoubleValue());
                break;
            case GROUPCODE_MLINE_STYLE_END_ANGLE:
                this.style.setEndAngle(var2.getDoubleValue());
                break;
            case GROUPCODE_MLINE_STYLE_ELEMENT_COLOR:
                if (this.processLineElement) {
                    this.element.setLineColor(var2.getIntegerValue());
                } else {
                    this.style.setFillColor(var2.getIntegerValue());
                }
                break;
            case GROUPCODE_MLINE_STYLE_FLAGS:
                this.style.setFlags(var2.getIntegerValue());
                break;
            default:
                super.parseCommonGroupCode(var1, var2, this.style);
        }

    }

    @Override
    public void startObject() {
        this.style = new DXFMLineStyle();
        this.processLineElement = false;
    }
}
